/*
 *Copyright (c) 2024 Black Sesame Technologies
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
*/
#include "ipc_hw_sem.h"

uint32 get_sem_lock(uint8 sem_id)
{
    uint32 data;
    un_reg_sem_u sem_reg;

    sem_reg.data = IPC_SEM_BASE_ADDR;
    sem_reg.bit.sem_id = sem_id;
    sem_reg.bit.bank_id = SEM_BANK_ID0;
    sem_reg.bit.mst_id = SEM_MST_ID1;

    do {
        data = REG_READ32(sem_reg.data);
    } while (data != 0);

    return data;
}

uint32 release_sem_lock(uint8_t sem_id)
{
    un_reg_sem_u sem_reg;

    sem_reg.data = IPC_SEM_BASE_ADDR;
    sem_reg.bit.sem_id = sem_id;
    sem_reg.bit.bank_id = SEM_BANK_ID0;
    sem_reg.bit.mst_id = SEM_MST_ID1;

    REG_WRITE32(sem_reg.data, 0x01);
    return 0;
}